﻿function Rectangle(position, size) {
    Node.apply(this, [document.createElementNS('http://www.w3.org/2000/svg', "rect"), position, size]);

    this.onUpdateLayout = function () {
        this._domEl.setAttribute("x", this.getActualPosition().x);
        this._domEl.setAttribute("y", this.getActualPosition().y);
        this._domEl.setAttribute("width", this._size.w);
        this._domEl.setAttribute("height", this._size.h);
        if (this.logFlag) {
            worker.postMessage("Create a container:  x:" + this._position.x + ", y:" + this._position.x);
        }
        this.logFlag = false;
    };

    this._domEl.style.fillOpacity = "0.5";
    this._domEl.style.fill = "white";
    this._domEl.style.stroke = "black";
    this._domEl.style.strokeWidth = "1";
    this._domEl.setAttribute("rx", "20");
    this._domEl.setAttribute("ry", "20");

    this.hitTest = function (pos) {
        var actualPosition = this.getActualPosition();
        if (pos.x > actualPosition.x
            && pos.y > actualPosition.y
            && pos.x < actualPosition.x + this.getSize().w
            && pos.y < actualPosition.y + this.getSize().h) {
            return new HitResult(true, actualPosition.x - pos.x, actualPosition.y - pos.y);
        }
        return new HitResult(false, 0, 0);
    }

    this.getCenterPos = function () {
        var x = this._position.x + (this._size.w) / 2;
        var y = this._position.y + (this._size.h) / 2;
        return { x: x, y: y };
    }
}